package petrochina.company.electric.ui.fragment;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatButton;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.google.gson.Gson;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.hjq.widget.layout.SettingBar;
import com.tencent.mmkv.MMKV;

import petrochina.company.electric.R;
import petrochina.company.electric.aop.SingleClick;
import petrochina.company.electric.app.TitleBarFragment;
import petrochina.company.electric.http.api.LoginApi;
import petrochina.company.electric.http.api.LogoutApi;
import petrochina.company.electric.http.glide.GlideApp;
import petrochina.company.electric.http.model.HttpData;
import petrochina.company.electric.ui.activity.HomeActivity;
import petrochina.company.electric.ui.activity.LoginActivity;

/**
 * desc   : 我的 Fragment
 */
public final class MineFragment extends TitleBarFragment<HomeActivity> {


    private ViewGroup mAvatarLayout;
    private ImageView mAvatarView;
    private SettingBar mPhoneView;
    private SettingBar mNameView;
    private AppCompatButton btnLogout;

    private LoginApi.Bean loginData;
    private MMKV mmkv;
    private Gson gson;

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment;
    }

    @Override
    protected void initView() {
        mmkv = MMKV.defaultMMKV();
        gson = new Gson();

        mAvatarLayout = findViewById(R.id.sb_mine_avatar);
        mAvatarView = findViewById(R.id.iv_mine_avatar);
        mNameView = findViewById(R.id.sb_mine_name);
        mPhoneView = findViewById(R.id.sb_mine_phone);
        btnLogout = findViewById(R.id.btn_logout);

        btnLogout.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        String loginStr = mmkv.getString("loginData", null);
        if (TextUtils.isEmpty(loginStr)) {
            return;
        }
        loginData = gson.fromJson(loginStr, LoginApi.Bean.class);
        if (loginData == null) {
            return;
        }

        btnLogout.setVisibility(View.VISIBLE);
        GlideApp.with(requireActivity())
                .load(R.drawable.avatar_placeholder_ic)
                .placeholder(R.drawable.avatar_placeholder_ic)
                .error(R.drawable.avatar_placeholder_ic)
                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                .into(mAvatarView);
        mNameView.setRightText(loginData.getStaffName());
        mPhoneView.setRightText("13212345678");

    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.btn_logout) {
            userLogout();
        }
    }

    private void userLogout() {
        EasyHttp.post(this)
                .api(new LogoutApi())
                .request(new HttpCallback<HttpData<LogoutApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<LogoutApi.Bean> result) {
                        ToastUtils.show("退出登陆成功");
                        mmkv.remove("loginData");
                        LoginActivity.start(getContext());
                        requireActivity().finish();
                        Log.d("ApplyFragment", "result = " + result.getData());
                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                    }

                });
    }

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return !super.isStatusBarEnabled();
    }
}